package com.zhixun.zhlq.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.zhixun.zhlq.entity.SysUserEntity;
import com.zhixun.zhlq.service.SysUserService;
import com.zhixun.zhlq.utils.BsResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 *
 *
 * @ClassName: SysUserController
 * @Author: hz
 * @Date: 2020-09-11 11:41:01
 * @Description:
 */
@Api(value = "LoginController", tags = "用户管理", description = "用户管理相关接口", position = 2)
@RestController
@RequestMapping("/sysuser")
@Slf4j
public class SysUserController extends BaseController{
    @Autowired
    private SysUserService sysUserService;



    @GetMapping("/getOne")
    public BsResult getOne(@RequestParam Long id){
        try {
            SysUserEntity sysUser = sysUserService.getById(id);
            return BsResult.success(sysUser);
        }catch (Exception e) {
            e.printStackTrace();
            return BsResult.error(500,e.getMessage());
        }
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    public BsResult save(SysUserEntity sysUser){
        try {
            sysUser.setCreateTime(new Date());
            sysUser.setCreateUserId(getUserId());
            boolean b = sysUserService.save(sysUser);
            return BsResult.success(b);
        } catch (Exception e) {
            e.printStackTrace();
            return BsResult.error(500,e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public BsResult update(SysUserEntity sysUser){
        try {
            sysUser.setUpdateUserId(getUserId());
            sysUser.setUpdateTime(new Date());
            boolean b = sysUserService.updateById(sysUser);
            return BsResult.success(b);
        } catch (Exception e) {
            e.printStackTrace();
            return BsResult.error(500,e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public BsResult delete(String ids){
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            SysUserEntity sysUser = new  SysUserEntity();
            boolean b = false;
            //软删除,
            for (String id:idList) {
                sysUser.setId(Long.valueOf(id));
                sysUser.setFlag(1);
                b = sysUserService.updateById(sysUser);
            }
            return BsResult.success(b);
        } catch (Exception e) {
            e.printStackTrace();
            return BsResult.error(500,e.getMessage());
        }

    }

}
